home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / archvrs / msdos / uuencode / uuencode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-11  |  2.3 KB  |  131 lines

  1. /* uuencode.c */
  2.      
  3. /*
  4. uudecode and uuencode are easily implemented under MSDOS as well.  Here
  5. are the sources for Microsoft C v3.0, but if you have another kind of C
  6. compiler, there should be perhaps only 1 change -- the output file of
  7. uudecode and the input file of uuencode must be in binary format.
  8. (ie.  binary files, like .EXE files may have byte patterns that are the
  9. same as ^Z, which signals end-of-file in non-binary (text) mode).
  10.  
  11.     Don Kneller
  12. UUCP:    ...ucbvax!ucsfcgl!kneller
  13. ARPA:    kneller@ucsf-cgl.ARPA
  14. BITNET:    kneller@ucsfcgl.BITNET
  15. */
  16.  
  17. #ifndef lint
  18. static char sccsid[] = "@(#)uuencode.c    5.1 (Berkeley) 7/2/83";
  19. #endif
  20.  
  21. /*
  22.  * uuencode [input] output
  23.  *
  24.  * Encode a file so it can be mailed to a remote system.
  25.  */
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29.  
  30. /* ENC is the basic 1 character encoding function to make a char printing */
  31. #define ENC(c) (((c) & 077) + ' ')
  32.  
  33. main(argc, argv)
  34. char **argv;
  35. {
  36.     FILE *in;
  37.     struct stat sbuf;
  38.     int mode;
  39.  
  40.     /* optional 1st argument */
  41.     if (argc > 2) {
  42. #ifdef MSDOS
  43.         /* Use binary mode */
  44.         if ((in = fopen(argv[1], "rb")) == NULL) {
  45. #else
  46.         if ((in = fopen(argv[1], "r")) == NULL) {
  47. #endif
  48.             perror(argv[1]);
  49.             exit(1);
  50.         }
  51.         argv++; argc--;
  52.     } else
  53.         in = stdin;
  54.  
  55.     if (argc != 2) {
  56.         printf("Usage: uuencode [infile] remotefile\n");
  57.         exit(2);
  58.     }
  59.  
  60.     /* figure out the input file mode */
  61.     fstat(fileno(in), &sbuf);
  62.     mode = sbuf.st_mode & 0777;
  63.     printf("begin %o %s\n", mode, argv[1]);
  64.  
  65.     encode(in, stdout);
  66.  
  67.     printf("end\n");
  68.     exit(0);
  69. }
  70.  
  71. /*
  72.  * copy from in to out, encoding as you go along.
  73.  */
  74. encode(in, out)
  75. FILE *in;
  76. FILE *out;
  77. {
  78.     char buf[80];
  79.     int i, n;
  80.  
  81.     for (;;) {
  82.         /* 1 (up to) 45 character line */
  83.         n = fr(in, buf, 45);
  84.         putc(ENC(n), out);
  85.  
  86.         for (i=0; i<n; i += 3)
  87.             outdec(&buf[i], out);
  88.  
  89.         putc('\n', out);
  90.         if (n <= 0)
  91.             break;
  92.     }
  93. }
  94.  
  95. /*
  96.  * output one group of 3 bytes, pointed at by p, on file f.
  97.  */
  98. outdec(p, f)
  99. char *p;
  100. FILE *f;
  101. {
  102.     int c1, c2, c3, c4;
  103.  
  104.     c1 = *p >> 2;
  105.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  106.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  107.     c4 = p[2] & 077;
  108.     putc(ENC(c1), f);
  109.     putc(ENC(c2), f);
  110.     putc(ENC(c3), f);
  111.     putc(ENC(c4), f);
  112. }
  113.  
  114. /* fr: like read but stdio */
  115. int
  116. fr(fd, buf, cnt)
  117. FILE *fd;
  118. char *buf;
  119. int cnt;
  120. {
  121.     int c, i;
  122.  
  123.     for (i=0; i<cnt; i++) {
  124.         c = getc(fd);
  125.         if (c == EOF)
  126.             return(i);
  127.         buf[i] = c;
  128.     }
  129.     return (cnt);
  130. }
  131.